package com.ofek.applicationbase.forms.question;

import java.io.Serializable;
import java.util.Calendar;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;

import com.ofek.applicationbase.R;
import com.ofek.applicationbase.forms.QuestionView;

public class TimeQuestionView extends QuestionView {
	// where we display the selected date and time
	protected TextView mDateDisplay;

	// date and time
	protected int mYear;
	protected int mMonth;
	protected int mDay;
	protected int mHour;
	protected int mMinute;
	protected boolean isFilled = false;
	private boolean showTime = true;
	private TextView question;
	private DatePickerDialog dpdialog;
	private TimePickerDialog tpdialog;
	private Object defaultAnswer;

	public TimeQuestionView(final int resourceId, final Activity context) {
		super(R.layout.form_question_time, context);
		initView(resourceId, context);
		initTime(context, System.currentTimeMillis());
	}

	private void initView(final int resourceId, final Activity context) {
		question = (TextView) findViewById(R.id.dateTitle);
		question.setText(context.getString(resourceId) + (isRequired() ? "*" : ""));

		mDateDisplay = (TextView) findViewById(R.id.dateDisplay);
		mDateDisplay.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(final View v, final MotionEvent event) {
				if (event.getAction() != MotionEvent.ACTION_UP)
					return false;
				if (!isFilled && defaultAnswer != null)
					initTime(getContext(), (Long) defaultAnswer);
				if (dpdialog == null)
					dpdialog = new DatePickerDialog(getContext(), new DatePickerDialog.OnDateSetListener() {

						@Override
						public void onDateSet(final DatePicker view, final int year, final int monthOfYear, final int dayOfMonth) {
							mYear = year;
							mMonth = monthOfYear;
							mDay = dayOfMonth;
							isFilled = true;
							broadcastAnswerChanged(getAnswer());
							updateDisplay();
						}
					}, mYear, mMonth, mDay);
				dpdialog.updateDate(mYear, mMonth, mDay);

				if (showTime) {
					if (tpdialog == null)
						tpdialog = new TimePickerDialog(getContext(), new TimePickerDialog.OnTimeSetListener() {

							@Override
							public void onTimeSet(final TimePicker view, final int hourOfDay, final int minute) {
								mHour = hourOfDay;
								mMinute = minute;
								broadcastAnswerChanged(getAnswer());
								updateDisplay();
							}

						}, mHour, mMinute, true);
					tpdialog.updateTime(mHour, mMinute);
					tpdialog.show();

				}
				dpdialog.show();
				return false;
			}
		});
	}

	private void initTime(final Context context, final long initTime) {
		final Calendar c = Calendar.getInstance();
		c.setTimeInMillis(initTime);
		mYear = c.get(Calendar.YEAR);
		mMonth = c.get(Calendar.MONTH);
		mDay = c.get(Calendar.DAY_OF_MONTH);
		mHour = c.get(Calendar.HOUR_OF_DAY);
		mMinute = c.get(Calendar.MINUTE);

		updateDisplay();
	}

	public void setShowTime(final boolean showTime) {
		this.showTime = showTime;
	}

	protected void updateDisplay() {
		if (!isFilled) {
			mDateDisplay.setText("Not Filled");
			return;
		}
		final Calendar instance = Calendar.getInstance();
		instance.set(mYear, mMonth, mDay, mHour, mMinute);
		instance.getTimeInMillis();
		final StringBuilder append = new StringBuilder()
		// Month is 0 based so add 1
				.append(mDay).append("/").append(mMonth + 1).append("/").append(mYear).append(" ");
		if (showTime)
			append.append(pad(mHour)).append(":").append(pad(mMinute));
		mDateDisplay.setText(append.toString());

	}

	@Override
	public void animateInvalidInput() {
		question.setTextColor(Color.RED);
		question.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.shake));
	}

	@Override
	protected void broadcastAnswerChanged(final Object newAnswer) {
		if (hasValidInput())
			question.setTextColor(Color.BLACK);
		super.broadcastAnswerChanged(newAnswer);
	}

	private static String pad(final int c) {
		if (c >= 10)
			return String.valueOf(c);
		else
			return "0" + String.valueOf(c);
	}

	@Override
	public Serializable getAnswer() {
		if (!isFilled)
			return null;
		final Calendar instance = Calendar.getInstance();
		instance.set(mYear, mMonth, mDay, mHour, mMinute);
		return instance.getTimeInMillis();
	}

	@Override
	public void setAnswer(final Object defaultAnswer) {
		isFilled = defaultAnswer != null;
		if (!isFilled)
			return;
		initTime(getContext(), (Long) defaultAnswer);
	}

	public void setTime(final long time) {

		initTime(getContext(), time);
	}

	@Override
	public void setEditable(final boolean edit) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setDefaultAnswer(final Object defaultAnswer, final boolean shouldPrePick) {
		isFilled = defaultAnswer != null && shouldPrePick || isFilled;
		this.defaultAnswer = defaultAnswer;

	}

}
